The game

The game

The game

The solution (1): simulation

set.seed(1234)
n_iter          = 1000
switch_choice   = rep(c(TRUE, FALSE), each = n_iter/2)
doors           = c(1,2,3)

The solution (1): simulation

set.seed(1234)
n_iter          = 1000
switch_choice   = rep(c(TRUE, FALSE), each = n_iter/2)
doors           = c(1,2,3)

The solution (1): simulation

games = NULL

for (i in switch_choice) {

  winning_door <- sample(doors, 1)
  players_original_choice <- sample(doors, 1)
  losing_doors <- setdiff(doors, winning_door)

  door_open <- ifelse(players_original_choice == winning_door,
                      sample(losing_doors, 1),
                      setdiff(losing_doors, players_original_choice))
  
  does_player_switch <- i
  
  players_final_choice <- ifelse(does_player_switch, 
                                 setdiff(doors, c(door_open, players_original_choice)), 
                                 players_original_choice)

  games_i <- data.frame(
    switched = does_player_switch, 
    won = players_final_choice == winning_door
  )
  
  games <- rbind.data.frame(games_i, games)
  
}

games |> 
  dplyr::mutate(
    switched = factor(switched, levels = c(TRUE, FALSE),  labels = c("Switched", "Did not switch")), 
    won = factor(won, levels = c(TRUE, FALSE), labels = c("Job! ", "Sack "))
  ) |> 
  
  dplyr::group_by(switched, won) |>  
  dplyr::summarise(n = n(), perc = n / (nrow(games)/2) * 100) |> 
  
  ggplot2::ggplot(aes(x = switched, y = perc, fill = won)) + 
  geom_col(position = position_dodge(width = 1), width = 0.95, alpha = .90) + 
  scale_fill_manual(values = c("#396e0a", "#880e0e")) + 
  labs(x = "\nDid the player switch their choice?", fill = "Job OR Sack? ", y = "Percent (%)\n") +
  coord_cartesian(ylim = c(0,100)) + 
  theme_minimal() +
  theme(legend.position = "top") 

The solution (1): simulation

The solution (2): enumeration